package com.baijia.util;

import org.springframework.beans.BeansException;
import org.springframework.beans.factory.NoSuchBeanDefinitionException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;

import java.lang.annotation.Annotation;

public class ServiceLocator implements ApplicationContextAware {

    private static ApplicationContext applicationContext = null;

    @Override
    public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
        ServiceLocator.applicationContext = applicationContext;
    }

    public static boolean containsBean(String arg0) {
        return applicationContext.containsBean(arg0);
    }

    public static String[] getAliases(String arg0) {
        return applicationContext.getAliases(arg0);
    }

    public static Object getBean(String arg0) throws BeansException {
        return applicationContext.getBean(arg0);
    }

    public static <T> T getBean(String arg0, Class<T> arg1) throws BeansException {
        return applicationContext.getBean(arg0, arg1);
    }

    public static Class<?> getType(String arg0) throws NoSuchBeanDefinitionException {
        return applicationContext.getType(arg0);
    }

    public static boolean isSingleton(String arg0) throws NoSuchBeanDefinitionException {
        return applicationContext.isSingleton(arg0);
    }

    public static <A extends Annotation> A findAnnotationOnBean(String beanName, Class<A> annotationType)
        throws BeansException {
        return applicationContext.findAnnotationOnBean(beanName, annotationType);
    }
}
